﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeighClient.Utils;

namespace WeighClient.Model
{
    public class DeviceInfo
    {
        public bool DoReadCard = false;
        public bool ReadCardSuccess = false;
        public bool WeightInterrupt = false;
        public bool Error = false;

        public Bill CurrentBill = null;
        public String CurrentGuid = "";

        private Double _weight = 0.0;
        private Double _tmp_weighing = -1.0;
        private bool _isSteady = false;

        public Double Weight
        {
            get { return _weight; }
            set
            {
                /*if (_weight != value)
                {
                    String time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    LastChangeTime = time;
                    _weight = value;
                }*/

                _weight = value;
                if (_tmp_weighing == -1.0)
                {
                    //初次加载，设置变量初始值
                    LastChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    _tmp_weighing = value;
                    _isSteady = false;
                }

                double dVal = System.Math.Abs(_tmp_weighing - value);
                double d2 = double.Parse(dVal.ToString("0.00"));
                if (d2 > App.GetConfig().WeighDifference)
                {
                    _isSteady = false;
                    LastChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    _tmp_weighing = value;
                }
                else
                {
                    int totalSeconds = Seconds();
                    if (totalSeconds >= App.GetConfig().SteadySeconds)
                    {
                        _isSteady = true;
                        LastChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        _tmp_weighing = value;
                    }
                }
            }
        }
        public String LastChangeTime { get; set; }

        public bool isSteady(int second)
        {
            return _isSteady;
            /*bool steady = false;

            int totalSeconds = Seconds();
            if (totalSeconds >= second)
            {
                return true;
            }

            return steady;*/
        }
        public int Seconds()
        {
            int seconds = 0;

            if (null != LastChangeTime)
            {
                DateTime dtStart = DateTime.ParseExact(LastChangeTime, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
                DateTime dtNow = DateTime.Now;

                TimeSpan ts = dtNow.Subtract(dtStart);
                int sec = (int)ts.TotalSeconds;

                return sec;
            }

            return seconds;
        }

        public PortControlHelper readCardCom = null;
        public WeightPortControlHelper weightCom = null;

        public IoComControlHelper ioComControlHelper = null;
    }
}
